package com.ebelter.ty.utils;


import android.text.TextUtils;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpecialText;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.AppConfig;
import com.ebelter.ty.R;

import java.util.Calendar;
import java.util.Date;

/**
 * 体脂的通用基本工具
 */

public class ScaleBaseUtils {



private static final String TAG="ScaleBaseUtils";

    //根据身高返回建议体重的最小值 height是cm 返回的是kg
    public static float getIdeaWeightMin(int height){
        return (float) (height * height * 0.0001 * 18.5);
    }
    //根据身高返回建议体重的最大值 height是cm 返回的是kg
    public static float getIdeaWeightMax(int height){
        return (float) (height * height * 0.0001 * 24.0);
    }


    public static String placeHolder2(String result,String str1,String str2){
        String local = AppUtils.getLocalStr();
        if (TextUtils.equals(local,AppUtils.iw)){
            //是要取反的
            return String.format(result,str1,str2);
        }else {
            //不需要取反的
            return String.format(result,str1,str2);
        }
    }


    //kg转磅
    public static float kg2lb(float kg){return (float) (kg * 144480.0/65536.0);}
    //磅转kg
    public static float lb2kg(float lb){
        return (float) (lb * 65536.0/144480.0);
    }


    //位置是否取反的适配方法
    public static String unitDisplay(String value,String unit){
        String local = AppUtils.getLocalStr();
        String result;
        if (TextUtils.equals(local,AppUtils.iw)){
            result =  value+unit;
            //是要取反的
            return result;
        }else {
            //不需要取反的
            result = value+unit;
            return result;
        }
    }

    public static float adapterDataFloat(float value){
        if (!AppConfig.getInstance().isKgModel()){
            return kg2lb(value);
        }
        return value;
    }


    //位置是否取反的适配方法
    public static String unitDisplay2(String value,String unit){
        String local = AppUtils.getLocalStr();
        String result;
        if (TextUtils.equals(local,AppUtils.iw)){
            result =  unit+value;//是要取反的
            return result;
        }else {
            //不需要取反的
            result = value+unit;
            return result;
        }
    }

    /**
     *
     * @param timeStr 1970-01-01 08:00:00 时间格式永远是这样的
     * @param fatmat 1年月日 2时分
     */
    public static String timeFatmat(String timeStr, int fatmat){
        if (TextUtils.isEmpty(timeStr)){
            return "";
        }
        if (timeStr.length()==10){
            //说明时间可能是1970-01-01
            timeStr=timeStr+" 00:00:00";
        }
        String result="";
        if (timeStr.length()!=19){
            LogUtils.i("ScaleBaseUtils",timeStr);
            return result;//说明传进来的时间格式是不正确的
        }
        //String string="1994-16-18 18:19:32";
        int year=NumUtils.string2Int(timeStr.substring(0,4));
        int month=NumUtils.string2Int(timeStr.substring(5,7));
        int day=NumUtils.string2Int(timeStr.substring(8,10));
        int h=NumUtils.string2Int(timeStr.substring(11,13));
        int m=NumUtils.string2Int(timeStr.substring(14,16));
        int s=NumUtils.string2Int(timeStr.substring(17,19));
        if (fatmat==1){
            result = SpecialText.formatDateStampString1(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==2){
            result=SpecialText.formatDateStampString2(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==3){
            result=SpecialText.formatDateStampString3(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==0){
            result = SpecialText.formatDateStampString0(new Date(year-1900,month-1,day,h,m,s).getTime());
        }
        LogUtils.i("ScaleBaseUtils","--fatmat="+fatmat+"----year="+year+"--month="+month+"--day="+day+"--h="+h+"--m="+m+"--s="+s+"\n"+result);

        return result;
    }



    //2018-16
    public static String intNumFatmat7(String days) {
        if (TextUtils.isEmpty(days)||days.length()!=7){
            return "";
        }
        int num=NumUtils.string2Int(days.substring(5,7));
        return SpecialText.numberLocle_i(CommonLib.APPContext,num);
    }

    public static String monthFatmat(String days) {
        if (TextUtils.isEmpty(days)||days.length()!=7){
            LogUtils.i(TAG,"格式化月出现错误数据--"+days);
            return "";
        }
        int num=NumUtils.string2Int(days.substring(5,7));
        if (num<=0){
            LogUtils.i(TAG,"格式化月出现错误数据--"+days);
            return "";

        }
        String[] monthTexts = CommonLib.APPContext.getResources().getStringArray(R.array.months);
        if (num>12){
            num=12;
            LogUtils.i(TAG,"格式化月出现错误数据--"+days);
        }
        return monthTexts[num-1];
    }
}
